var AppManager = function(){
	var appGridObj = null;
	var validator = null;
	function resetForm(rowIndex){
		var record =  appGridObj.getRecord(rowIndex);
		$("#id").val(record["appId"]);
		$("#appName").val(record["appName"]);
		$("#sort").val(record["sort"]);
		$("#description").val(record["description"]);
		if(record["display"] == "1"){
			$("#display").attr("checked",true);
		}else{
			$("#display").attr("checked",false);
		}
		
	}
	function saveAppInfo(index,layero){
		validator.form();
		if (!validator.valid()) {
			return false;
		}
		var applicationDTO ={
				"appId":$("#id").val()==""?null:$("#id").val(),
				"appName":$("#appName").val(),
				"sort":parseInt($("#sort").val()),
				"description":$("#description").val(),
				"display": $("#display").is(':checked')==true?1:0
		}
		SysApplicationController.saveApplicationInfo(applicationDTO,function(data){
			if(data){
				Message.success("保存成功！");
				Message.close(index);
				appGridObj.refreshPage();
			}else{
				Message.error("保存失败！");
			}
		});
	}
	return {
		initAppTable:function(){
			validator =$("#applicationEdit").validate({
				onkeyup: function(element, event){
					if ($(element).attr('name') == "appName") {
                        return false; 
                    } else { 
                        if ( event.which === 9 && this.elementValue( element ) === "" ) {
                            return;
                        } else if ( element.name in this.submitted || element === this.lastElement ) {
                            this.element( element );
                        }
                    }
				},
				rules: {
					appName: {required: true,maxlength:6,remote:{
						url:contextPath+'/sys/app/checkNameISExist.htm',
						type:'post',
						cache:false,
						dataType:'json',
						data:{
							'appId':function(){
								return jQuery("#id").val();
							}
								
						}
					}},
					sort :{required:true,maxlength:3,digits:true},
					description :{required:true,maxlength:50}
				},
				messages:{
					appName: {remote:jQuery.format("应用名【{0}】已经存在")}
				}
			});
			appGridObj = $.fn.bsgrid.init('appTable', {
	            url:contextPath+"/sys/app/list.htm",
	            pageSizeSelect: true,
	            stripeRows: true,
	            rowSelectedColor:false,
	            pageSize: 10
				
	        });
		},
		newApp:function(){
			//解决validator缓存问题
			AppUtils.validatorRemoteCache("#appName");
			AppUtils.clearForm("#applicationEdit");
			DialogUtils.innerDialog({
				area: ['500px', '380px'],
			    title: '添加应用',
			    content: $("#editApplication"),
			    btn: ['保存', '取消'],
			    yes: function(index, layero){
			    	saveAppInfo(index,layero);
			    },cancel: function(index){
			    }
			});
		},
		displayFormatter:function(record, rowIndex, colIndex, options){
			if(record['display'] == "1"){
				return "显示";
			}else{
				return "隐藏";
			}
		},
		appOperate:function(record, rowIndex, colIndex, options){
			return '<a href="#" onclick="AppManager.editApp(\''+rowIndex+'\');">编辑</a>&nbsp;&nbsp;<a href="#" onclick="AppManager.removeApp(\''+appGridObj.getRecordIndexValue(record, 'appId')+'\');">删除</a>';
		},
		editApp:function(rowIndex){
			AppUtils.validatorRemoteCache("#appName");
			AppUtils.clearForm("#applicationEdit");
			resetForm(rowIndex);
			DialogUtils.innerDialog({
			    area: ['500px', '380px'],
			    title: '编辑应用',
			    content: $("#editApplication"),
			    btn: ['保存', '取消'],
			    yes: function(index, layero){
			    	saveAppInfo(index,layero);
			    },cancel: function(index){
			    }
			});
		},
		removeApp:function(appId){
			Message.confirm("确定删除该应用吗？",function(index){
				SysApplicationController.removeAppByAppId(appId,function(data){
					if(data){
						Message.success("删除成功！");
						Message.close(index);
						appGridObj.refreshPage();
					}else{
						Message.error("删除失败！");
					}
					
				});
				
			});
		}
	}
}();